package com.zhiyi.app.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyi.app.dao.AppInfoMapper;
import com.zhiyi.app.entity.AppInfo;
import com.zhiyi.app.service.AppInfoService;
import lombok.extern.log4j.Log4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Log4j
public class AppInfoServiceImpl implements AppInfoService {
    @Autowired
    private AppInfoMapper appInfoMapper;
    @Override
    public PageInfo list(AppInfo appinfo, Integer page, Integer limit) {
//        进行物理分页
        PageHelper.startPage(page,limit);
//        数据库进行查询
        List<AppInfo> list=appInfoMapper.list(appinfo);
        PageInfo info=new PageInfo(list);
        log.info("查询完毕");
        return info;
    }

    @Override
    public HSSFWorkbook excel(AppInfo appinfo) {
//        创建一个excel
        HSSFWorkbook workbook=new HSSFWorkbook();
//        创建sheet
        HSSFSheet sheet= workbook.createSheet("APP信息");
//        创建row
        HSSFRow row= sheet.createRow(0);
//        创建cell
        HSSFCell cell= row.createCell(0, CellType.STRING);
        cell.setCellValue("软件名称");
        cell= row.createCell(1, CellType.STRING);
        cell.setCellValue("APK名称");
        cell= row.createCell(2, CellType.STRING);
        cell.setCellValue("软件大小");
//        从数据库里拿出数据
        List<AppInfo> list=appInfoMapper.list(appinfo);
        for (int i=0;i<list.size();i++){
            row= sheet.createRow(i+1);
//        创建cell
            cell= row.createCell(0, CellType.STRING);
            cell.setCellValue(list.get(i).getSoftwarename());
            cell= row.createCell(1, CellType.STRING);
            cell.setCellValue(list.get(i).getApkname());
            cell= row.createCell(2, CellType.STRING);
            cell.setCellValue(String.valueOf(list.get(i).getSoftwaresize()) );
        }
        return workbook;
    }




    @Override
    public Boolean add(AppInfo appinfo) {
        /**
         * 设置创建时间
         */
        appinfo.setCreationdate(new Date());
        /**
         * 设置修改时间
         */
        appinfo.setModifydate(new Date());
        /**
         * 设置应用状态
         */
        appinfo.setStatus(1L);
        /**
         * 初始化下载量
         */
        appinfo.setDownloads(0L);
        return appInfoMapper.insert(appinfo)>0?true:false;
    }

    @Override
    public AppInfo updateBefore(Integer id) {
        return appInfoMapper.selectByPrimaryKey(Long.valueOf(id));
    }

    @Override
    public Boolean update(AppInfo appinfo) {
        appinfo.setModifydate(new Date());
        return appInfoMapper
                .updateByPrimaryKeySelective(appinfo)>0?true:false;
    }
}
